/**
 * Created by LMN on 2016/7/26.
 */
var express = require('express');
var router = express.Router();
var db = require('../mod/seq.js');
var err = require('../mod/err.js');
var crypto = require('crypto');
var config = require('../config');
var socketIO = require('../mod/io.js');

//user login
router.post('/', function(req, res){
    var data = req.body;
    if(!data) {
        res.json(err.format_err);
    }else if(!data.username||!data.password){
        res.json(err.need_name_password);
    }else if(data.password&&data.password.length<6){
        res.json(err.password_six);
    }else{
        db.user.find({
            where: {
                username: data.username,
                password: crypto.createHash('sha1').update(data.password).digest('hex')
            }
        }).then(function(msg){
            if(msg&&msg.username){
                req.session.userId = msg.id;
                req.session.username = msg.username;
                req.session.auth = msg.auth;
                res.json({
                    auth: msg.auth
                });
            }else{
                res.json(err.error_name_password);
            }
        });
    }
});

module.exports = router;